﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.ComponentModel;
using System.Windows.Input;
using System.ComponentModel.Composition;

namespace StoryMaker.Common
{
    [Export]
    public class ShapeViewTemplates : NotifyPropertyChangedBase
    {
        [ImportingConstructor]
        public ShapeViewTemplates([ImportMany(typeof(Brush))]Brush[] availableBrushes, [ImportMany(typeof(StoryShape))] StoryShape[] templateShapes)
        {
            this._availableBrushes = new List<Brush>(availableBrushes);
            this._shapes = new List<StoryShape>(templateShapes);
        }
        private double _defaultHeight = 50;
        [DefaultValue(50)]
        public double DefaultHeight
        {
            get { return _defaultHeight; }
            set
            {
                if (!EqualityComparer<double>.Default.Equals(_defaultHeight, value))
                {
                    _defaultHeight = value;
                    NotifyPropertyChanged("DefaultHeight");
                }
            }
        }

        private double _defaultWidth = 50;
        [DefaultValue(50)]
        public double DefaultWidth
        {
            get { return _defaultWidth; }
            set
            {
                if (!EqualityComparer<double>.Default.Equals(_defaultWidth, value))
                {
                    _defaultWidth = value;
                    NotifyPropertyChanged("DefaultWidth");
                }
            }
        }

        private bool _defaultFillShapes = true;
        [DefaultValue(true)]
        public bool DefaultFillShapes
        {
            get { return _defaultFillShapes; }
            set
            {
                if (!EqualityComparer<bool>.Default.Equals(_defaultFillShapes, value))
                {
                    _defaultFillShapes = value;
                    if (_shapes != null)
                    {
                        _shapes.ForEach(x => x.FillShape = DefaultFillShapes);
                    }
                    NotifyPropertyChanged("DefaultFillShapes");
                }
            }
        }

        private Brush _defaultBrush = Brushes.Red;
        public Brush DefaultBrush
        {
            get { return _defaultBrush; }
            set
            {
                if (!EqualityComparer<Brush>.Default.Equals(_defaultBrush, value))
                {
                    _defaultBrush = value;
                    _shapes.ForEach(x => x.Brush = DefaultBrush);
                    NotifyPropertyChanged("DefaultBrush");
                }
            }
        }

        private List<Brush> _availableBrushes;
        public IEnumerable<Brush> AvailableBrushes
        {
            get { return _availableBrushes; }
        }

        private ICommand setDefaultBrush;
        public ICommand SetDefaultBrush
        {
            get
            {
                if (setDefaultBrush == null)
                {
                    setDefaultBrush = new GenericCommand<Brush>(x => DefaultBrush = x);
                }
                return setDefaultBrush;
            }
        }

        private StoryShape _filledShape;
        public StoryShape FilledShape
        {
            get
            {
                if (_filledShape == null)
                {
                    _filledShape = Shapes.First();
                }
                return _filledShape;
            }
        }

        private StoryShape _unfilledShape;
        public StoryShape UnfilledShape
        {
            get
            {
                if (_unfilledShape == null)
                {
                    _unfilledShape = Shapes.First();
                }
                return _unfilledShape;
            }
        }

        private Func<object, bool> _canDoDrop;
        public Func<object, bool> CanDoDrop
        {
            get
            {
                if (_canDoDrop == null)
                {
                    _canDoDrop = x => false;
                }
                return _canDoDrop;
            }
        }

        private Action<object> _dragOut;
        public Action<object> DragOut
        {
            get
            {
                if (_dragOut == null)
                {
                    _dragOut = new Action<object>(dragOut);
                }
                return _dragOut;
            }
        }

        private void dragOut(object newShape)
        {
            StoryShape shape = newShape as StoryShape;
            if (shape != null)
            {
                newShape = shape.Clone();
            }
        }

        private List<StoryShape> _shapes;
        public IEnumerable<StoryShape> Shapes
        {
            get
            {
                return _shapes;
            }
        }
    }
}
